package com.ibeetl.admin.aod.service.excel.match;

import com.ibeetl.admin.aod.entity.*;
import com.ibeetl.admin.aod.service.*;
import com.ibeetl.admin.aod.service.excel.parser.*;
import com.ibeetl.admin.aod.service.excel.processor.data.BatchInsertDataProcessor;
import com.ibeetl.admin.aod.util.DateUtils;
import com.ibeetl.admin.aod.util.ThreadPoolFactory;
import com.ibeetl.admin.aod.util.ToolUtil;
import com.ibeetl.admin.aod.web.dto.AodIndustryCompositeDto;
import com.ibeetl.admin.aod.web.dto.AodIndustryDataDto;
import com.ibeetl.admin.aod.web.dto.AodIndustryReplyDataDto;
import com.ibeetl.admin.aod.web.query.AodBlackListQuery;
import com.ibeetl.admin.aod.web.query.AodIndustryQuery;
import com.ibeetl.admin.aod.web.query.AodNumberAttributionQuery;
import com.ibeetl.admin.aod.web.query.FilterQuery;
import com.ibeetl.admin.core.util.PlatformException;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 待拆分成不同的策略
 * 待重构 TODO
 */
@Service
public class MatchManagerImpl implements MatchManager {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    protected AodIndustryDataService aodIndustryDataService;

    @Autowired
    protected TornadoFileParser tornadoFileParser;

    @Autowired
    protected FarFileParser farFileParser;

    @Autowired
    protected ReplyFileParser replyFileParser;

    @Autowired
    protected DieImportTaskService dieImportTaskService;

    @Autowired
    protected AodNumberAttributionService aodNumberAttributionService;

    @Autowired
    protected AodIndustryKeywordService aodIndustryKeywordService;

    @Autowired
    protected FilterFileParser filterFileParser;

    @Autowired
    Environment env;

    @Autowired
    protected AodBlackListService aodBlackListService;

    @Autowired
    protected AodIndustryService aodIndustryService;

    @Autowired
    @Qualifier("defaultJdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    @Autowired
    protected BatchInsertDataProcessor batchInsertDataProcessor;

    private AtomicInteger counter = new AtomicInteger(1);

    public String getBatchNo() {
        DateFormat dateTimeFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        if (counter.get() > 9) {
            counter.set(1);
        }
        return dateTimeFormat.format(new Date()) + counter.getAndIncrement();
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    protected void beginTask(DieImportTask dieImportTask) {
        dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.IN_THE_PROCESSING.getValue());
        dieImportTask.setTaskStartTime(new Date());
        dieImportTaskService.update(dieImportTask);
    }

    /**
     * 数据量比较大的时候 入库后 数据库中做比较
     * @param batchNo
     * @param fileList
     */
    @Override
    @Deprecated
    public void matchTwoFile2DB(String batchNo, List<File> fileList) {

        DieImportTask dieImportTask = dieImportTaskService.findByBatchNo(batchNo);

        try {
            // 先判断任务是否被废弃了
            if (dieImportTask.getPhysicsFlag() == DieImportTask.ABANDONED) {
                // 任务已废弃,跳过
                return;
            }

            // 任务开始
            beginTask(dieImportTask);

            // 此处全量取,待优化 TODO
            List<AodNumberAttribution> aodNumberAttributionList = aodNumberAttributionService.queryAll(new AodNumberAttributionQuery());
            Map<String,AodNumberAttribution> aodNumberAttributionMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(aodNumberAttributionList)) {
                for (AodNumberAttribution aodNumberAttribution : aodNumberAttributionList) {
                    aodNumberAttributionMap.put(aodNumberAttribution.getPNum(), aodNumberAttribution);
                }
            }

            Map<String,List<AodIndustryCompositeDto>> aodIndustryDataDtoSendMap = parseFile(fileList.get(0), aodNumberAttributionMap);

            Map<String,List<AodIndustryCompositeDto>> aodIndustryDataDtoReplyMap = parseFile(fileList.get(1), aodNumberAttributionMap);

            if (null != aodIndustryDataDtoSendMap) {
                Set<String> keySet = aodIndustryDataDtoSendMap.keySet();
                for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
                    String key = iterator.next();
                    if (null == aodIndustryDataDtoSendMap.get(key).get(0).getAodIndustryDataDto()) {
                        // 设置主模板
                        Map<String,List<AodIndustryCompositeDto>> aodIndustryDataDtoMap = aodIndustryDataDtoSendMap;
                        aodIndustryDataDtoSendMap = aodIndustryDataDtoReplyMap;
                        aodIndustryDataDtoReplyMap = aodIndustryDataDtoMap;
                        break;
                    }
                }

            }
            if (null == aodIndustryDataDtoSendMap || null == aodIndustryDataDtoReplyMap) {
                throw new Exception("存在空文件,请仔细检查模板是否正确");
            }

            Map<String,List<AodIndustryDataDto>> resultMap = new HashMap<>();

            String sensitiveInfo = env.getProperty("aod.sensitiveInfo",String.class);
            Properties prop=new Properties();
            prop.load(new InputStreamReader(MatchManagerImpl.class.getClassLoader().getResourceAsStream("out.properties"), "UTF-8"));

            sensitiveInfo = prop.getProperty("aod.sensitiveInfo");
            int timeLimit = env.getProperty("aod.timeLimit",Integer.class);

            for (Map.Entry<String,List<AodIndustryCompositeDto>> entry : aodIndustryDataDtoSendMap.entrySet()) {

                String phoneNumber = entry.getKey();
                List<AodIndustryDataDto> aodIndustryDataDtoList = new ArrayList<>();

                for (AodIndustryCompositeDto sendDataDto : entry.getValue()) {

                    AodIndustryDataDto aodIndustrySendDataDto = sendDataDto.getAodIndustryDataDto();

                    if (aodIndustryDataDtoReplyMap.containsKey(phoneNumber)) {
                        // 有对应的回复模板 回复模板里面只取第一条
                        AodIndustryReplyDataDto replyDataDto = aodIndustryDataDtoReplyMap.get(phoneNumber).get(0).getAodIndustryReplyDataDto();
                        compareObject(aodIndustrySendDataDto, replyDataDto, sensitiveInfo, timeLimit);

                    } else {
                        // 没有回复模板 等级设置为 3
                        aodIndustrySendDataDto.setLevel(AodIndustryData.Level.THREE.getValue());

                    }
                    aodIndustryDataDtoList.add(aodIndustrySendDataDto);
                }

                resultMap.put(phoneNumber, aodIndustryDataDtoList);

            }

            log.info("开始写结果文件");
            // 写结果文件
            writeResultFile(resultMap, dieImportTask.getFileSavePath());
            log.info("写结果文件结束");

            dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.PROCESSED_SUCCESS.getValue());
            dieImportTask.setResultFileNames(DieImportTask.DELIVRD_RESULT_FILE_NAME + "," + DieImportTask.OTHER_RESULT_FILE_NAME);
        } catch (Exception e) {
            log.error("excel配对失败：",e);
            dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.PROCESSED_FAILED.getValue());
            dieImportTask.setProcessResult(e.getMessage().length() > 500 ? e.getMessage().substring(0,500) : e.getMessage());
        } finally {
            postProcessTask(dieImportTask);
        }
    }


    /**
     * excel匹配
     * @param batchNo
     * @param fileList
     */
    @Override
    public void matchTwoFile(String batchNo, List<File> fileList) {

        DieImportTask dieImportTask = dieImportTaskService.findByBatchNo(batchNo);

        try {
            // 先判断任务是否被废弃了
            if (dieImportTask.getPhysicsFlag() == DieImportTask.ABANDONED) {
                // 任务已废弃,跳过
                return;
            }

            // 任务开始
            beginTask(dieImportTask);

            // 此处全量取,待优化
            List<AodNumberAttribution> aodNumberAttributionList = aodNumberAttributionService.queryAll(new AodNumberAttributionQuery());
            Map<String,AodNumberAttribution> aodNumberAttributionMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(aodNumberAttributionList)) {
                for (AodNumberAttribution aodNumberAttribution : aodNumberAttributionList) {
                    aodNumberAttributionMap.put(aodNumberAttribution.getPNum(), aodNumberAttribution);
                }
            }

            Map<String,List<AodIndustryCompositeDto>> aodIndustryDataDtoSendMap = parseFile(fileList.get(0), aodNumberAttributionMap);

            Map<String,List<AodIndustryCompositeDto>> aodIndustryDataDtoReplyMap = parseFile(fileList.get(1), aodNumberAttributionMap);

            if (null != aodIndustryDataDtoSendMap) {
                Set<String> keySet = aodIndustryDataDtoSendMap.keySet();
                for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
                    String key = iterator.next();
                    if (null == aodIndustryDataDtoSendMap.get(key).get(0).getAodIndustryDataDto()) {
                        // 设置主模板
                        Map<String,List<AodIndustryCompositeDto>> aodIndustryDataDtoMap = aodIndustryDataDtoSendMap;
                        aodIndustryDataDtoSendMap = aodIndustryDataDtoReplyMap;
                        aodIndustryDataDtoReplyMap = aodIndustryDataDtoMap;
                        break;
                    }
                }

            }
            if (null == aodIndustryDataDtoSendMap || null == aodIndustryDataDtoReplyMap) {
                throw new Exception("存在空文件,请仔细检查模板是否正确");
            }
            log.info("发送模板的记录数为" + aodIndustryDataDtoSendMap.size());
            log.info("回复模板的记录数为" + aodIndustryDataDtoReplyMap.size());
            long mb = 1024 * 1024;
            log.info("当前JVM最大内存为：" + Runtime.getRuntime().maxMemory() / mb);
            log.info("当前JVM已使用内存为：" + Runtime.getRuntime().totalMemory() / mb);
            log.info("当前JVM可使用内存为：" + Runtime.getRuntime().freeMemory() / mb);

            log.info("开始对比");
            Map<String,List<AodIndustryDataDto>> resultMap = new HashMap<>();

            String sensitiveInfo = env.getProperty("aod.sensitiveInfo",String.class);
            Properties prop=new Properties();
            prop.load(new InputStreamReader(MatchManagerImpl.class.getClassLoader().getResourceAsStream("out.properties"), "UTF-8"));

            sensitiveInfo = prop.getProperty("aod.sensitiveInfo");
            int timeLimit = env.getProperty("aod.timeLimit",Integer.class);

                for (Map.Entry<String,List<AodIndustryCompositeDto>> entry : aodIndustryDataDtoSendMap.entrySet()) {

                    String phoneNumber = entry.getKey();
                    List<AodIndustryDataDto> aodIndustryDataDtoList = new ArrayList<>();

                    for (AodIndustryCompositeDto sendDataDto : entry.getValue()) {

                        AodIndustryDataDto aodIndustrySendDataDto = sendDataDto.getAodIndustryDataDto();

                        if (aodIndustryDataDtoReplyMap.containsKey(phoneNumber)) {
                            // 有对应的回复模板 回复模板里面只取第一条
                            AodIndustryReplyDataDto replyDataDto = aodIndustryDataDtoReplyMap.get(phoneNumber).get(0).getAodIndustryReplyDataDto();
                            compareObject(aodIndustrySendDataDto, replyDataDto, sensitiveInfo, timeLimit);

                        } else {
                            // 没有回复模板 等级设置为 3
                            aodIndustrySendDataDto.setLevel(AodIndustryData.Level.THREE.getValue());

                        }
                        aodIndustryDataDtoList.add(aodIndustrySendDataDto);
                    }

                    resultMap.put(phoneNumber, aodIndustryDataDtoList);

            }

            log.info("对比结束");

            log.info("开始写结果文件");
            // 写结果文件
            writeResultFile(resultMap, dieImportTask.getFileSavePath());
            log.info("写结果文件结束");

            dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.PROCESSED_SUCCESS.getValue());
            dieImportTask.setResultFileNames(DieImportTask.DELIVRD_RESULT_FILE_NAME + "," + DieImportTask.OTHER_RESULT_FILE_NAME);
        } catch (Exception e) {
            log.error("excel配对失败：",e);
            dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.PROCESSED_FAILED.getValue());
            dieImportTask.setProcessResult(e.getMessage().length() > 500 ? e.getMessage().substring(0,500) : e.getMessage());
        } finally {
            postProcessTask(dieImportTask);
        }

    }

    private String resultFileHeader = "账号,手机号码,时间,内容,地区,供应商,回复,标签,等级,归属地ID";

    private String fileEncoding = "gbk";

    private void write(BufferedWriter bufferedWriter, AodIndustryDataDto aodIndustryDataDto) throws Exception {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(ToolUtil.nullToString(aodIndustryDataDto.getAccount()) + ",")
                     .append(ToolUtil.nullToString(aodIndustryDataDto.getPhoneNumber()) + ",")
                     .append(DateUtils.formatToDateTime(aodIndustryDataDto.getSendTime()) + ",")
                     .append(ToolUtil.nullToString(aodIndustryDataDto.getSendContent()) + ",");
        if (null != aodIndustryDataDto.getAodNumberAttribution()) {
            stringBuilder.append(aodIndustryDataDto.getAodNumberAttribution().getProvince() + aodIndustryDataDto.getAodNumberAttribution().getCity() + ",")
                         .append(aodIndustryDataDto.getAodNumberAttribution().getCorp() + ",");
        } else {
            stringBuilder.append(",,");
        }
        stringBuilder.append(ToolUtil.nullToString(aodIndustryDataDto.getReplyContent()) + ",")
                     .append(ToolUtil.nullToString(aodIndustryDataDto.getLabel()) + ",")
                     .append(ToolUtil.nullToString(aodIndustryDataDto.getLevel()) + ",")
                     .append(ToolUtil.nullToString(aodIndustryDataDto.getNumberAttribution()) + ",");
        bufferedWriter.write(stringBuilder.toString() + "\r\n");

    }

    private void writeResultFile(Map<String,List<AodIndustryDataDto>> resultMap, String fileSavePath) throws Exception {
        BufferedWriter delivrdWriter = null;
        BufferedWriter mkWriter = null;
        try {
            delivrdWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileSavePath + "/" + DieImportTask.DELIVRD_RESULT_FILE_NAME, false), fileEncoding));
            delivrdWriter.write(resultFileHeader + "\r\n");
            mkWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileSavePath + "/" + DieImportTask.OTHER_RESULT_FILE_NAME, false), fileEncoding));
            mkWriter.write(resultFileHeader + "\r\n");

            for (Map.Entry<String,List<AodIndustryDataDto>> entry : resultMap.entrySet()) {
                List<AodIndustryDataDto> aodIndustryDataDtoList = entry.getValue();
                for (AodIndustryDataDto aodIndustryDataDto : aodIndustryDataDtoList) {
                    if (aodIndustryDataDto.getStatus().equalsIgnoreCase("DELIVRD") || aodIndustryDataDto.getStatus().equalsIgnoreCase("0")) {
                        write(delivrdWriter, aodIndustryDataDto);
                    } else {
                        write(mkWriter, aodIndustryDataDto);
                    }
                }
            }

        } catch (Exception e) {
            throw e;
        } finally {
            if (null != delivrdWriter) {
                delivrdWriter.flush();
                delivrdWriter.close();
            }
            if (null != mkWriter) {
                mkWriter.flush();
                mkWriter.close();
            }
        }

    }

    private void compareObject(AodIndustryDataDto sendDataDto, AodIndustryReplyDataDto replyDataDto, String sensitiveInfo, int timeLimit) {

        int betweenDay = DateUtils.differentDaysByMillisecond(sendDataDto.getSendTime(), replyDataDto.getReplyTime());
        if (betweenDay <= timeLimit) {
            sendDataDto.setReplyContent(replyDataDto.getReplyContent());
            sendDataDto.setReplyTime(replyDataDto.getReplyTime());

            if (replyDataDto.getReplyContent().equalsIgnoreCase("y")) {
                sendDataDto.setLevel(AodIndustryData.Level.ONE.getValue());
            } else if (StringUtils.isEmpty(replyDataDto.getReplyContent())) {
                sendDataDto.setLevel(AodIndustryData.Level.THREE.getValue());
            } else if (sensitiveInfo.contains(replyDataDto.getReplyContent())) {
                sendDataDto.setLevel(AodIndustryData.Level.FOUR.getValue());
            } else {
                sendDataDto.setLevel(AodIndustryData.Level.TWO.getValue());
            }
        } else {
            // 不符合条件的 等级设置为 3
            sendDataDto.setLevel(AodIndustryData.Level.THREE.getValue());
        }
    }

    @Override
    public void matchByCsv(File file, FilterQuery filterQuery, String batchNo) {

        DieImportTask dieImportTask = dieImportTaskService.findByBatchNo(batchNo);

        try {
            // 先判断任务是否被废弃了
            if (dieImportTask.getPhysicsFlag() == DieImportTask.ABANDONED) {
                // 任务已废弃,跳过
                return;
            }

            // 任务开始
            beginTask(dieImportTask);

            log.info(String.format("开始执行批次号为【%s】的任务", batchNo));

            String keywordIds = filterQuery.getKeywordId();
            String[] keywordArr = keywordIds.split(",");
            List<AodIndustryKeyword> aodIndustryKeywordList = new ArrayList<>();

            log.info("查询关键词开始");
            for (String keywordId : keywordArr) {
                aodIndustryKeywordList.add(aodIndustryKeywordService.queryById(keywordId));
            }
            log.info("查询关键词结束");

            // 查询出所有的黑名单
            List<String> phoneNumberList = aodBlackListService.queryAllPhoneNumber(new AodBlackListQuery());

            log.info("所有的行业表名开始");
            // 所有的行业表名
            List<AodIndustry> aodIndustryList = aodIndustryService.queryList(new AodIndustryQuery());
            Map<Long,AodIndustry> aodIndustryMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(aodIndustryList)) {
                for (AodIndustry aodIndustry : aodIndustryList) {
                    aodIndustryMap.put(aodIndustry.getId(), aodIndustry);
                }
            }
            log.info("所有的行业表名结束");

            String account = filterQuery.getAccount();

            Map<String,List<AodIndustryData>> aodIndustryDataMap = new HashMap<>();

            log.info("解析文件开始");
            InputStreamReader inputStreamReader = null;
            BufferedReader bufferedReader = null;
            try {
                inputStreamReader = new InputStreamReader(new FileInputStream(file), fileEncoding);
                bufferedReader = new BufferedReader(inputStreamReader);
                // 读取头信息
                String line = bufferedReader.readLine();

                while ((line = bufferedReader.readLine()) != null) {
                    AodIndustryData aodIndustryData = filterFileParser.parseFilter(line, aodIndustryKeywordList, account);
                    if (null != aodIndustryData && !phoneNumberList.contains(aodIndustryData.getPhoneNumber())) {
                        // 匹配不为空 并且 不包含在黑名单中
//                        aodIndustryDataList.add(aodIndustryData);
                        String tableName = aodIndustryMap.get(aodIndustryData.getIndustryId()).getIndustryTable();
                        if (aodIndustryDataMap.containsKey(tableName)) {
                            aodIndustryDataMap.get(tableName).add(aodIndustryData);
                        } else {
                            List<AodIndustryData> tmpAodIndustryDataList = new ArrayList<>();
                            tmpAodIndustryDataList.add(aodIndustryData);
                            aodIndustryDataMap.put(tableName, tmpAodIndustryDataList);
                        }
                    }
                }

                // 处理入库
                if (aodIndustryDataMap.size() > 0) {
                    // 按照表名去分配list

                    boolean result = batchInsertDataProcessor.insertMapData(aodIndustryDataMap);
                    if (!result) {
                        throw new PlatformException("插入数据失败");
                    }

                }
                log.info(String.format("执行批次号为【%s】的任务结束", batchNo));
            } catch (Exception e) {
                throw e;
            } finally {
                if (null != bufferedReader) {
                    bufferedReader.close();
                }
                if (null != inputStreamReader) {
                    inputStreamReader.close();
                }
            }

            dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.PROCESSED_SUCCESS.getValue());

        } catch (Exception e) {
            log.error("excel筛查失败：",e);
            dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.PROCESSED_FAILED.getValue());
            dieImportTask.setProcessResult(e.getMessage().length() > 500 ? e.getMessage().substring(0,500) : e.getMessage());
        } finally {
            postProcessTask(dieImportTask);
        }

    }

    private String buildLikeSql(String keyword, boolean isLike) {

        String retVal;
        if (isLike) {
            retVal = " or instr(send_content,'" + keyword + "') > 0";
        } else {
            retVal = " or send_content = '" + keyword + "'";
        }
        return retVal;
    }

    @Override
    public void matchByDB(FilterQuery filterQuery, String batchNo) {

        DieImportTask dieImportTask = dieImportTaskService.findByBatchNo(batchNo);

        Connection connection = null;

        try {
            // 先判断任务是否被废弃了
            if (dieImportTask.getPhysicsFlag() == DieImportTask.ABANDONED) {
                // 任务已废弃,跳过
                return;
            }

            // 任务开始
            beginTask(dieImportTask);

            log.info("开始进行筛查");

            log.info("查询关键词开始");
            Long selfIndustryId = filterQuery.getIndustryId();

            boolean isLike = "1".equalsIgnoreCase(env.getProperty("aod.matchFlag"));

            String keywordIds = filterQuery.getKeywordId();
            String[] keywordArr = keywordIds.split(",");
            StringBuilder likeSql = new StringBuilder();
            for (String keywordId : keywordArr) {
                AodIndustryKeyword aodIndustryKeyword = aodIndustryKeywordService.queryById(keywordId);
                likeSql.append(buildLikeSql(aodIndustryKeyword.getKeyword(), isLike));
            }
            if (likeSql.length() > 0) {
                likeSql = new StringBuilder(likeSql.substring(likeSql.indexOf("or") + 2));
            }
            log.info("查询关键词结束");

            AodIndustry destTable = aodIndustryService.queryById(selfIndustryId);

            connection = jdbcTemplate.getDataSource().getConnection();
            connection.setAutoCommit(false);
//            jdbcTemplate.getDataSource().getConnection().setAutoCommit(false);

            // 取出
            List<AodIndustry> aodIndustryList = aodIndustryService.queryList(new AodIndustryQuery());
            String account = filterQuery.getAccount();

            if (CollectionUtils.isNotEmpty(aodIndustryList)) {
                for (AodIndustry aodIndustry : aodIndustryList) {
                    if (aodIndustry.getId() == selfIndustryId) {
                        // 关键词所属的行业类别表 不需要处理
                        continue;
                    }
                    log.info("开始匹配行业表名{}的数据", aodIndustry.getIndustryTable());
                    // 把标签列 符合关键词的 移动数据到对应的 行业数据表中
                    String moveSql;
                    if (StringUtils.isNotEmpty(account)) {
                        moveSql = String.format(env.getProperty("aod.moveConditionSql"), destTable.getIndustryTable(), aodIndustry.getIndustryTable(), likeSql.toString(), account);
                    } else {
                        moveSql = String.format(env.getProperty("aod.moveSql"), destTable.getIndustryTable(), aodIndustry.getIndustryTable(), likeSql.toString());
                    }
                    connection.prepareStatement(moveSql).execute();
                    log.info(moveSql);
//                    jdbcTemplate.execute(moveSql);
                    // 更新对应的行业ID
                    String updateSql = String.format(env.getProperty("aod.updateSql"), destTable.getIndustryTable(), destTable.getId());
                    connection.prepareStatement(updateSql).execute();
                    log.info(updateSql);
//                    jdbcTemplate.execute(updateSql);
                    log.info("匹配行业表名{}的数据结束", aodIndustry.getIndustryTable());
                }
            }
            if (filterQuery.getIndustryFlag() == 2) {
                log.info("开始删除无行业表中匹配的数据");
                // 无行业筛查 删除无行业表中对应的数据
                String deleteSql;
                if (StringUtils.isNotEmpty(account)) {
                    deleteSql = String.format(env.getProperty("aod.deleteConditionSql"), likeSql.toString(), account);
                } else {
                    deleteSql = String.format(env.getProperty("aod.deleteSql"), likeSql.toString());
                }
                connection.prepareStatement(deleteSql).execute();
                log.info(deleteSql);
//                jdbcTemplate.execute(deleteSql);
                log.info("删除无行业表中匹配的数据结束");
            }

            connection.commit();

            dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.PROCESSED_SUCCESS.getValue());

            log.info("筛查结束");

        } catch (Exception e) {
            log.error("筛查失败：",e);
            try {
                if (null != connection) {
                    connection.rollback();
                }
            } catch (Exception e1) {
                log.error("回滚jdbcTemplate的事务失败",e1);
            }

            dieImportTask.setProcessStatus(DieImportTask.ProcessStatus.PROCESSED_FAILED.getValue());
            dieImportTask.setProcessResult(e.getMessage().length() > 500 ? e.getMessage().substring(0,500) : e.getMessage());
        } finally {
            postProcessTask(dieImportTask);
        }

    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    private void postProcessTask(DieImportTask dieImportTask) {
        Date now = new Date();
        dieImportTask.setTaskEndTime(now);
        int consumingTime = ToolUtil.nullToInt(now.getTime() / 1000 - dieImportTask.getTaskStartTime().getTime() / 1000);
        dieImportTask.setConsumingTime(consumingTime);
        dieImportTaskService.update(dieImportTask);
    }

    private void parseSendFile(BufferedReader bufferedReader, Map<String,List<AodIndustryCompositeDto>> resultMap, FileParser fileParser, Map<String,AodNumberAttribution> aodNumberAttributionMap) throws Exception {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            AodIndustryCompositeDto aodIndustryCompositeDto = new AodIndustryCompositeDto();

            AodIndustryDataDto aodIndustryDataDto = fileParser.parse(line, aodNumberAttributionMap);
            if (null == aodIndustryDataDto) {
                continue;
            }
            aodIndustryCompositeDto.setAodIndustryDataDto(aodIndustryDataDto);

            if (resultMap.containsKey(aodIndustryDataDto.getPhoneNumber())) {
                resultMap.get(aodIndustryDataDto.getPhoneNumber()).add(aodIndustryCompositeDto);
            } else {
                List<AodIndustryCompositeDto> aodIndustryCompositeDtos = new ArrayList<>();
                aodIndustryCompositeDtos.add(aodIndustryCompositeDto);
                resultMap.put(aodIndustryDataDto.getPhoneNumber(), aodIndustryCompositeDtos);
            }
        }
    }

    private void parseReplyFile(BufferedReader bufferedReader, Map<String,List<AodIndustryCompositeDto>> resultMap, FileParser fileParser) throws Exception {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            AodIndustryCompositeDto aodIndustryCompositeDto = new AodIndustryCompositeDto();

            AodIndustryReplyDataDto aodIndustryReplyDataDto = fileParser.parseReply(line);
            aodIndustryCompositeDto.setAodIndustryReplyDataDto(aodIndustryReplyDataDto);

            if (resultMap.containsKey(aodIndustryReplyDataDto.getPhoneNumber())) {
                resultMap.get(aodIndustryReplyDataDto.getPhoneNumber()).add(aodIndustryCompositeDto);
            } else {
                List<AodIndustryCompositeDto> aodIndustryCompositeDtos = new ArrayList<>();
                aodIndustryCompositeDtos.add(aodIndustryCompositeDto);
                resultMap.put(aodIndustryReplyDataDto.getPhoneNumber(), aodIndustryCompositeDtos);
            }
        }
    }

    /**
     * 解析直接入库
     * @param file
     * @param aodNumberAttributionMap
     * @return
     * @throws Exception
     */
    public Map<String,List<AodIndustryCompositeDto>> parseFile2DB(File file, Map<String,AodNumberAttribution> aodNumberAttributionMap) throws Exception {
        Date startDate = new Date();
        log.info("开始时间：" + startDate);

        Map<String,List<AodIndustryCompositeDto>> resultMap = new HashMap<>();

        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream(file), fileEncoding);
            bufferedReader = new BufferedReader(inputStreamReader);
            String line = bufferedReader.readLine();
            FileParser fileParser = null;
            if (line.startsWith(FileParser.TORNADO_TEMPLATE)) {
                fileParser = tornadoFileParser;
            } else if (line.startsWith(FileParser.FAR_TEMPLATE)) {
                fileParser = farFileParser;
            } else if (line.startsWith(FileParser.REPLY_TEMPLATE)) {
                fileParser = replyFileParser;
            }
            if (null == fileParser) {
                throw new Exception("模板错误,找不到对应的模板解析器");
            }
            if (fileParser == replyFileParser) {
                parseReplyFile(bufferedReader, resultMap, fileParser);
            } else {
                parseSendFile(bufferedReader, resultMap, fileParser, aodNumberAttributionMap);
            }

            Date endDate = new Date();
            log.info("结束时间：" + endDate);
            log.info("解析文件耗时：" + (endDate.getTime() - startDate.getTime()) + "毫秒");
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != bufferedReader) {
                bufferedReader.close();
            }
            if (null != inputStreamReader) {
                inputStreamReader.close();
            }
        }

        return resultMap;
    }

    public Map<String,List<AodIndustryCompositeDto>> parseFile(File file, Map<String,AodNumberAttribution> aodNumberAttributionMap) throws Exception {
        Date startDate = new Date();
        log.info("开始时间：" + startDate);

        Map<String,List<AodIndustryCompositeDto>> resultMap = new HashMap<>();

        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            inputStreamReader = new InputStreamReader(new FileInputStream(file), fileEncoding);
            bufferedReader = new BufferedReader(inputStreamReader);
            String line = bufferedReader.readLine();
            FileParser fileParser = null;
            if (line.startsWith(FileParser.TORNADO_TEMPLATE)) {
                fileParser = tornadoFileParser;
            } else if (line.startsWith(FileParser.FAR_TEMPLATE)) {
                fileParser = farFileParser;
            } else if (line.startsWith(FileParser.REPLY_TEMPLATE)) {
                fileParser = replyFileParser;
            }
            if (null == fileParser) {
                throw new Exception("模板错误,找不到对应的模板解析器");
            }
            if (fileParser == replyFileParser) {
                parseReplyFile(bufferedReader, resultMap, fileParser);
            } else {
                parseSendFile(bufferedReader, resultMap, fileParser, aodNumberAttributionMap);
            }

            Date endDate = new Date();
            log.info("结束时间：" + endDate);
            log.info("解析文件耗时：" + (endDate.getTime() - startDate.getTime()) + "毫秒");
        } catch (Exception e) {
            throw e;
        } finally {
            if (null != bufferedReader) {
                bufferedReader.close();
            }
            if (null != inputStreamReader) {
                inputStreamReader.close();
            }
        }

        return resultMap;
    }


    @Override
    public List<File> convertMultipartFileList2FileList(List<MultipartFile> multipartFiles) throws Exception {

        if (CollectionUtils.isEmpty(multipartFiles)) {
            return null;
        }

        List<File> fileList = new ArrayList<>(multipartFiles.size());
        String saveFilePath = System.getProperty("user.dir") + "/excelSave/" + System.currentTimeMillis();
        FileUtils.forceMkdir(new File(saveFilePath));

        for (MultipartFile file : multipartFiles) {
            File tempFile = convertMultipartFile2File(saveFilePath, file);
            fileList.add(tempFile);
        }
        return fileList;
    }

    @Override
    public File convertMultipartFile2File(String saveFilePath, MultipartFile multipartFile) throws Exception {
        File tempFile = new File(saveFilePath + "/" + multipartFile.getOriginalFilename());
        multipartFile.transferTo(tempFile);
        return tempFile;
    }


}
